/*
 * Copyright 2007-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 * 
 * Project: gtinyurl
 */
package org.gnut.tinyurl.portlet.servlet;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.portlet.PortletResponse;
import javax.portlet.PortletURL;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * 
 * @author tungt
 * 
 */
public class TinyHttpServletResponse extends HttpServletResponseWrapper {
	private static String DEFAULT_HOST = "localhost";
	private static String DEFAULT_PROTOCAL = "http";
	private PortletResponse portletResponse;

	/**
	 * 
	 * @param response
	 * @param request
	 * @param portletResponse
	 */
	public TinyHttpServletResponse(HttpServletResponse response,
			HttpServletRequest request, PortletResponse portletResponse) {
		super(response);
		this.portletResponse = portletResponse;

	}

	public static Map getQueryMap(String query) {
		if (query != null) {
			String[] params = query.split("&");
			Map map = new HashMap();
			for (int i = 0; i < params.length; i++) {
				String[] tempParam = params[i].split("=");

				String name = params[i].split("=")[0];
				String value = "";
				if (tempParam.length > 1) {
					value = params[i].split("=")[1];
				}
				map.put(name, value);
			}
			return map;
		}
		return null;
	} /*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServletResponseWrapper#encodeURL(java.lang.String)
	 */

	public String encodeURL(String url) {
		Map map = null;
		String path = "";
		try {
			URL tempURL = new URL(DEFAULT_PROTOCAL, DEFAULT_HOST, url);
			String query = tempURL.getQuery();
			path = tempURL.getPath();
			map = getQueryMap(query);
		} catch (MalformedURLException e) {

		}
		PortletURL portletURL = ((RenderResponse) portletResponse)
				.createActionURL();
		portletURL.setParameter("__send_redirect__", super.encodeURL(path));
		if (map != null) {
			Set entries = map.entrySet();
			for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
				Entry entry = (Entry) iterator.next();
				portletURL.setParameter((String) entry.getKey(), (String) entry
						.getValue());
			}
		}
		String uri = portletURL.toString();
		return uri;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServletResponseWrapper#sendRedirect(java.lang.
	 * String)
	 */
	public void sendRedirect(String uri) throws IOException {
		String url="";
		if(!url.startsWith("http")){
			PortletURL portletURL = ((RenderResponse) portletResponse)
					.createActionURL();
			portletURL.setParameter("__send_redirect__", (uri));
			url = portletURL.toString();
		}
		getWriter().write(
				"<script type=\"text/javascript\">window.location=\"" + url
						+ "\"</script>");
	}

}
